<script setup>
import {User, Lock} from '@element-plus/icons-vue'
import {ref, reactive} from 'vue'

const ruleForm = reactive({
  username: '',
  password: ''
})

//用于注册的表单校验模型
const registerDataRules = ref({
  username: [
    {required: true, message: '请输入用户名', trigger: 'blur'}
  ],
  password: [
    {required: true, message: '请输入密码', trigger: 'blur'}
  ]
})

import {adminLoginService} from '@/api/admin.js'
import router from "@/router/index.js";
import {ElMessage} from "element-plus";
import {useTokenStore} from "@/stores/token.js";
const userTokenStore = useTokenStore()
//登录按钮功能实现
const loginButton = async () => {
  const response =  await adminLoginService(ruleForm)
  //存储token
  userTokenStore.setToken(response.data.token)
  ElMessage.success("登录成功")
  const resolvedRoute = routers(response.data.adminPermissionsList);
  await router.push(resolvedRoute);
}

const routers = (data) => router.resolve({
  path: '/admin',
  query: {
    data: data
  }
})


</script>

<template>
  <el-row class="login-page">
    <el-col :span="12" class="bg"></el-col>
    <el-col :span="6" :offset="3" class="form">
      <!-- 登录表单 -->
      <el-form ref="form" size="large" :model="ruleForm" autocomplete="off" :rules="registerDataRules">
        <el-form-item>
          <h1>管理员登录</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input :prefix-icon="User" v-model="ruleForm.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input :prefix-icon="Lock" v-model="ruleForm.password" type="password"
                    placeholder="请输入密码"></el-input>
        </el-form-item>
        <!-- 登录按钮 -->
        <el-form-item>
          <el-button class="button" type="primary" auto-insert-space @click="loginButton">
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>

</template>

<style lang="scss" scoped>
/* 样式 */
.login-page {
  height: 100vh;
  background-color: #fff;

  .bg {
    background: url('https://blog-majun.oss-cn-beijing.aliyuncs.com/book-long.jpg') no-repeat center / auto;
    //border-radius: 0 20px 20px 0;
  }

  .form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    user-select: none;

    .title {
      margin: 0 auto;
    }

    .button {
      width: 100%;
    }

    .flex {
      width: 100%;
      display: flex;
      justify-content: space-between;
    }
  }
}
</style>
